#include "GameManager.h"

GameManager::GameManager(int w, int h, std::string title)
: m_running(true), m_window(sf::VideoMode(w, h, 32), title), m_numStates(0), m_currentState(0)
{

}

GameManager::~GameManager()
{
    for(int i = 0; i < m_states.size(); i++)
    {
        if(m_states[i])
            delete m_states[i];
    }
    m_states.clear();
}

void GameManager::init()
{

}

void GameManager::update()
{
    m_states[m_currentState]->update(this);
}

void GameManager::handleEvents()
{
    m_window.GetEvent(m_event);
    m_states[m_currentState]->handleEvents(this);
}

void GameManager::render()
{
    m_window.Clear();
    m_states[m_currentState]->render(this);
    m_window.Display();
}

void GameManager::popState()
{
    if(m_states.back())
        delete m_states.back();
    m_states.pop_back();                                          /// maybe a problem
    m_numStates--;
}

void GameManager::pushState(State* s)
{
    m_numStates++;
    m_states.push_back(s);
    m_states.back()->init(m_numStates - 1);
    m_currentState = m_numStates -1;

}

void GameManager::setActiveState(int state)
{
    if(state < m_numStates)                                      /// check for < or <= see what fits
        m_currentState = state;
}


void GameManager::draw(sf::Sprite& s)
{
    m_window.Draw(s);
}
